const gulp = require("gulp");
const sass = require("gulp-sass")(require("sass"));
const connect = require("gulp-connect");
//合并
const concat = require("gulp-concat");
//重命名
const rename = require("gulp-rename");
//压缩
const uglify = require("gulp-uglify");
//转ES5
const babel = require("gulp-babel");
//压缩CSS
const cleanCss = require("gulp-clean-css");
const sourcemaps = require("gulp-sourcemaps");

gulp.task("copyHtml", (done) => {
  gulp.src("*.html").pipe(gulp.dest("dist")).pipe(connect.reload());
  done();
});
gulp.task("sass", (done) => {
  gulp
    .src("sass/*.scss")
    .pipe(sourcemaps.init())

    .pipe(
      sass({
        outputStyle: "compressed",
      })
    )
    .pipe(sourcemaps.write(""))
    .pipe(gulp.dest("dist/css"))

    .pipe(connect.reload());
  done();
});
gulp.task("copyJs", (done) => {
  gulp
    .src("js/*.js")
    .pipe(gulp.dest("dist/js"))
    .pipe(uglify())
    .pipe(
      rename({
        suffix: "-min",
      })
    )
    .pipe(gulp.dest("dist/js"))
    .pipe(connect.reload());
  done();
});
gulp.task("copyImg", (done) => {
  gulp.src("images/**").pipe(gulp.dest("dist/images")).pipe(connect.reload());
  done();
});
gulp.task("watch", (done) => {
  gulp.watch("*.html", gulp.series("copyHtml"));
  gulp.watch("sass/*.scss", gulp.series("sass"));
  gulp.watch("js/*.js", gulp.series("copyJs"));
  gulp.watch("images/**", gulp.series("copyImg"));
  done();
});
gulp.task("server", (done) => {
  connect.server({
    root: "dist",
    livereload: true,
  });
  done();
});
gulp.task("default", gulp.series("server", "watch"));
